<?php
    class Admincp_SlideController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_validate;
        
        public function init()
        {
            $this->loadTemplates('admin');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admincp_Model_Slide();
            $this->_validate = new Admincp_Form_ValidateSlide();
            $this->view->headTitle('Quản lý ảnh slide');
        }
        
        public function preDispatch()
        {
            $auth = Zend_Auth::getInstance();
            if(!$auth->hasIdentity()) {
                if($this->_request->getActionName() != 'login')
                    $this->_redirect('admincp/index/login');
            }
        }
        
        public function indexAction()
        {
            $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->listItem());
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(10);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data = $paginator;
			$this->view->total = $paginator->getTotalItemCount();

            if($_POST['add']) {
                $this->_redirect('admincp/slide/create');
            }
            if($_POST['delete']) {
                $listId = $this->_arrParam['check'];
			    $this->_model->deleteMultiItem($listId);
			    $this->_redirect($_SERVER['HTTP_REFERER']);
            }
        }
        
        public function createAction()
        {
            if($this->_request->isPost()) {
                $upload = new Zend_File_Transfer_Adapter_Http();
                $info   = $upload->getFileInfo();
                $image  = $upload->getFileName('image', false);
                $flag   = true;
                
                $upload->addValidator('Size', false, 4800000)
                       ->addValidator('Extension', false, 'jpg,jpeg,png,gif');
                if(!$upload->isValid()) {
                    $this->view->errorImage = 'Ảnh phải đúng định dạng jpg, jpeg, png, gif, kích thước 1100 x 450px và dung lượng tối đa 4 MB';
                    $flag = false;
                }
                if(!$this->_validate->validImage($image)) {
                    $this->view->errorImage = 'Ảnh phải đúng định dạng jpg, jpeg, png, gif, kích thước 1100 x 450px và dung lượng tối đa 4 MB';
                    $flag = false;
                }
                if($flag == true) {
                    $image = strtolower($image);
                    $resize = new Image_Resize();
                    preg_match('#\.([^\.]+)$#', $image, $matches);
                    $fileExtension  = $matches[1];
                    $fileName = 'pic_' . time() . mt_rand(5, 99999999);
                    $newFileName = $fileName . '.' . $fileExtension;
                    $image_info = getimagesize($info['image']['tmp_name']);
                    $resize->load($info['image']['tmp_name']);
                    $resize->resize(1100, 450);
                    $resize->save(BASE_PATH.'/public/files/slide/'.$newFileName);
                    
                    $data = array(
                        'image' => $newFileName,
                        'status' => 1,
                    );
                    $this->_model->saveItem($data, 'create');
                    $this->_redirect('admincp/slide');
                }
            }
        }
        
        public function deleteAction()
        {
            $id = (int) $this->_arrParam['id'];
		    $this->_model->deleteItem($id);
		    $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function publishAction()
		{
		    $id = (int) $this->_arrParam['id'];
		    $status = (int) $this->_arrParam['status'];
		    $status = ($status == 1) ? 2 : 1;
		    $data = array(
                'id' => $id,
		        'status' => $status,
		    );
		    $this->_model->saveItem($data, 'update');
		    $this->_redirect($_SERVER['HTTP_REFERER']);
		}
    }
